//
//  DataBase.m
//  AIM
//
//  Created by user10 on 2017/9/29.
//  Copyright © 2017年 user10. All rights reserved.
//

#import "DataBase.h"
#import <FMDB.h>
@interface DataBase()<NSCopying,NSMutableCopying>{
    
    FMDatabase  *_db;
    
}
@end

@implementation DataBase

static DataBase *_DBCtl = nil;
+(instancetype)sharedDataBase{
    
    if (_DBCtl == nil) {
        
        _DBCtl = [[DataBase alloc] init];
        
        [_DBCtl initDataBase];
        
    }
    
    return _DBCtl;
    
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    
    if (_DBCtl == nil) {
        
        _DBCtl = [super allocWithZone:zone];
        
    }
    
    return _DBCtl;
    
}

-(id)copy{
    
    return self;
    
}

-(id)mutableCopy{
    
    return self;
    
}

-(id)copyWithZone:(NSZone *)zone{
    
    return self;
    
}

-(id)mutableCopyWithZone:(NSZone *)zone{
    
    return self;
    
}


-(void)initDataBase{
    // 获得Documents目录路径
    
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 文件路径
    
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"log.sqlite"];
    NSLog(@"%@",filePath);
    
    // 实例化FMDataBase对象
    
    _db = [FMDatabase databaseWithPath:filePath];
    
    [_db open];
    
    // 初始化数据表
    NSString *personSql = @"CREATE TABLE if not exists 'log' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL ,'time' VARCHAR(255),'opration' VARCHAR(255),'type' VARCHAR(255),'filename'VARCHAR(255)) ";
    
    
    [_db executeUpdate:personSql];
    [_db close];
    
}

- (void)addLogModel:(logModel *)logModel{
    [_db open];
    
    NSNumber *maxID = @(0);
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM log"];
    //获取数据库中最大的ID
    while ([res next]) {
        if ([maxID integerValue] < [[res stringForColumn:@"id"] integerValue]) {
            maxID = @([[res stringForColumn:@"id"] integerValue] ) ;
        }
        
    }
    maxID = @([maxID integerValue] + 1);
    
    [_db executeUpdate:@"INSERT INTO log(id,time,opration,type,filename)VALUES(?,?,?,?,?)",maxID,logModel.time,logModel.opration,logModel.type,logModel.filename];
    
    [_db close];
    
}

- (NSMutableArray *)getAllLog{
    
    [_db open];
    
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM log"];
    
    while ([res next]) {
        logModel *log = [[logModel alloc] init];
        log.time = [res stringForColumn:@"time"];
        log.opration = [res stringForColumn:@"opration"];
        log.type = [res stringForColumn:@"type"];
        log.filename = [res stringForColumn:@"filename"];
        [dataArray addObject:log];
    }
    [_db close];
    return dataArray;
}

- (NSMutableArray *)getResultLogWithStartTime:(NSString *)stratTime endTime:(NSString *)endTime{
    
    [_db open];
    
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    NSString *str = [NSString stringWithFormat:@"select * FROM log where time >= '%@' and time <= '%@'",stratTime,endTime];
    FMResultSet *res = [_db executeQuery:str];
    
    while ([res next]) {
        logModel *log = [[logModel alloc] init];
        log.time = [res stringForColumn:@"time"];
        log.opration = [res stringForColumn:@"opration"];
        log.type = [res stringForColumn:@"type"];
        log.filename = [res stringForColumn:@"filename"];
        [dataArray addObject:log];
    }
    [_db close];
    return dataArray;
}

- (void)delete30Day{
    [_db open];
    
    NSString *str = [NSString stringWithFormat:@"DELETE FROM log WHERE date('now', '-7 day') >= date(CreatedTime);"];
    [_db executeQuery:str];
   
    [_db close];
    
}
@end
